package com.tangruojie.stream;

import com.tangruojie.entity.Author;
import com.tangruojie.entity.Book;

import java.util.Comparator;
import java.util.List;

/**
 * 终结操作之 max & min 最大 & 最小
 */
public class StreamDemo11 {

    public static void main(String[] args) {

        List<Author> authors = StreamDemoTest.getAuthors();

        /**
         * 打印最高分书籍名
         */
        authors.stream()
                .flatMap(author -> author.getBooks().stream())
                .max(new Comparator<Book>() {
                    @Override
                    public int compare(Book o1, Book o2) {
                        return o1.getScore() - o2.getScore();
                    }
                })
                .stream()
                .distinct()
                .forEach(book -> System.out.println(book.getName()));

        /**
         * 打印书籍最高分
         */
        authors.stream()
                .flatMap(author -> author.getBooks().stream())
                .map(Book::getScore)
                .max(Integer::compareTo)
                .stream()
                .distinct()
                .forEach(System.out::println);

        /**
         * 打印最低分书籍名
         */
        authors.stream()
                .flatMap(author -> author.getBooks().stream())
                .min((book1, book2) -> book1.getScore() - book2.getScore())
                .stream()
                .distinct()
                .forEach(book -> System.out.println(book.getName()));

        /**
         * 获取书籍最低分
         */
        authors.stream()
                .flatMap(author -> author.getBooks().stream())
                .map(Book::getScore)
                .min(Integer::compareTo)
                .stream()
                .distinct()
                .forEach(System.out::println);

    }

}
